<template>
  <div>
    <Main v-if="show" :type="'form'">
      <Title :title="title" />
      <el-form
        ref="form"
        :model="form"
        :rules="form"
        label-width="150px"
        class="demos-forms"
      >
        <el-form-item
          label="活动名称"
          prop="name"
          :rules="{
            required: true,
            message: '请输入活动名称',
            trigger: 'blur'
          }"
        >
          <el-input
            v-model="form.name"
            class="form-input"
            size="medium"
            placeholder="请输入活动名称"
            maxlength="10"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item
          label="活动时间"
          prop="time"
          :rules="{
            required: true,
            message: '请输入活动时间',
            trigger: 'blur'
          }"
        >
          <el-date-picker
            v-model="form.time"
            type="datetimerange"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动类型" :rules="{ required: true }">
          <el-radio v-model="form.type" :label="1" size="medium"
            >普通团</el-radio
          >
          <el-radio v-model="form.type" :label="2" size="medium"
            >老带新团</el-radio
          >
        </el-form-item>
        <el-form-item label="参与用户" :rules="{ required: true }">
          <el-radio v-model="form.join_users_auth" :label="1" size="medium"
            >不限制</el-radio
          >
          <el-radio v-model="form.join_users_auth" :label="2" size="medium"
            >新用户</el-radio
          >
        </el-form-item>
        <el-form-item
          label="活动封面"
          prop="file"
          :rules="{
            required: true,
            message: '请添加活动封面',
            trigger: 'blur'
          }"
        >
          <div class="commodityImage clearfix">
            <div v-if="form.file" class="icon">
              <img
                :src="$store.state.qiniu + form.file + $store.state.imgRotate"
              />
            </div>
            <el-button
              type="primary"
              @click="
                showImages = true;
                useType = 'img';
              "
              >选择图片</el-button
            >
          </div>
        </el-form-item>
        <el-form-item label="活动说明">
          <el-input
            v-model="form.content"
            type="textarea"
            :autosize="{ minRows: 6, maxRows: 8 }"
            placeholder="请输入内容"
            style="width:50%;"
            size="medium"
            maxlength="100"
            show-word-limit
          ></el-input
          ><span style="color:#909399;font-size:12px;">
            可输入100字，不填则不显示</span
          >
        </el-form-item>
        <el-form-item label="推荐拼团">
          <el-checkbox v-model="form.is_recommend">开启</el-checkbox>
          <span style="color:#909399;font-size:12px;"
            >开启后，商品详情中开团区域显示其他人发起的拼团，用户可以直接点击参与</span
          >
        </el-form-item>
        <el-form-item
          label="成团有效时间"
          prop="join_valid_time"
          :rules="{
            required: true,
            message: '请输入成团有效时间',
            trigger: 'blur'
          }"
        >
          <el-input-number
            v-model="form.join_valid_time"
            :precision="0"
            :step="1"
            :min="0"
            :max="2000"
            :controls="false"
            size="medium"
          ></el-input-number>
          小时
          <span style="color:#909399;font-size:12px;">不超过2000小时</span>
        </el-form-item>
        <el-form-item
          label="订单支付有效期"
          prop="pay_valid_time"
          :rules="{
            required: true,
            message: '订单支付有效期',
            trigger: 'blur'
          }"
        >
          <el-input-number
            v-model="form.pay_valid_time"
            :precision="0"
            :step="1"
            :min="1"
            :controls="false"
            size="medium"
          ></el-input-number>
          分钟
        </el-form-item>
        <!-- <el-form-item label='下单可用优惠'>
            <el-checkbox-group v-model="form.available_yh">
              <el-checkbox label="1">积分</el-checkbox>
              <el-checkbox label="2">余额</el-checkbox>
            </el-checkbox-group>
          </el-form-item> -->
        <!-- <el-form-item label='团长是否优惠'>
            <el-radio-group v-model="form.leader_discount">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="2">否</el-radio>
            </el-radio-group>
            <br />
            <el-radio-group v-model="form.leader_discount_content"
                            v-if="form.leader_discount === 1">
              <el-radio :label="1">免单</el-radio>
              <el-radio :label="2">优惠80%</el-radio>
              <el-radio :label="3">优惠50%</el-radio>
            </el-radio-group>
          </el-form-item> -->
      </el-form>

      <Footer>
        <el-button plain @click="$emit('out')">取 消</el-button>
        <el-button
          v-if="title == '新建拼团'"
          type="primary"
          @click="submitForm('add')"
          >保存并添加商品</el-button
        >
        <el-button v-else type="primary" @click="submitForm('edit')"
          >保存并编辑商品</el-button
        >
      </Footer>
      <images
        :dialog-images="showImages"
        :use-btn="true"
        :use-type="'img'"
        @close="close"
      />
    </Main>
    <shop v-else :assemble-id="assembleId" :types="types" @out="$emit('out')" />
  </div>
</template>
<script>
import { SET_ASSEMBLE } from "../service";

export default {
  components: {
    images: r =>
      require.ensure([], () => r(require("publicCom/imgupdate")), "imgupdate"),
    shop: () => import("./component/shop")
  },
  props: {
    dataVal: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      title: "新建拼团",
      showImages: false,
      form: {
        name: "",
        type: 1,
        join_users_auth: 1,
        file: "",
        time: "",
        stime: "",
        etime: "",
        join_valid_time: "",
        pay_valid_time: "",
        content: "",
        is_recommend: false,
        available_yh: [],
        leader_discount: 2,
        leader_discount_content: ""
      },
      show: true,
      assembleId: "",
      assemble_idE: "",
      types: "",
      slof: ""
    };
  },
  created() {
    if (JSON.stringify(this.dataVal) !== "{}") {
      this.title = "编辑拼团";
      this.assemble_idE = this.dataVal.assemble_id;
      this.form = {
        name: this.dataVal.name,
        type: this.dataVal.type,
        join_users_auth: this.dataVal.join_users_auth,
        file: this.dataVal.pic,
        time: [this.dataVal.stime, this.dataVal.etime],
        stime: this.dataVal.stime,
        etime: this.dataVal.etime,
        join_valid_time: this.dataVal.join_valid_time,
        pay_valid_time: this.dataVal.pay_valid_time,
        content: this.dataVal.content,
        is_recommend: this.dataVal.is_recommend === 1,
        available_yh: this.dataVal.available_yh
          ? this.dataVal.available_yh.split(",")
          : [],
        leader_discount: this.dataVal.leader_discount,
        leader_discount_content: this.dataVal.leader_discount_content
      };
    } else {
      this.title = "新建拼团";
    }
  },

  methods: {
    close(file) {
      this.showImages = false;
      if (!file) return;
      this.form.file = file.file_path;
    },
    async submitForm(type) {
      let prams = {
        name: this.form.name,
        type: this.form.type,
        join_users_auth: this.form.join_users_auth,
        file: this.form.file,
        stime: this.form.time[0],
        etime: this.form.time[1],
        join_valid_time: this.form.join_valid_time,
        pay_valid_time: this.form.pay_valid_time,
        content: this.form.content,
        is_recommend: this.form.is_recommend ? 1 : 0,
        available_yh: this.form.available_yh.join(","),
        assemble_id: this.assemble_idE,
        leader_discount: this.form.leader_discount,
        leader_discount_content: this.form.leader_discount_content
      };
      await this.$refs["form"].validate(valid => {
        if (valid) {
          SET_ASSEMBLE(prams).then(({ code, data, msg }) => {
            if (code === 200) {
              this.show = false;
              this.types = type;
              this.assembleId = data;
            }
            this.$message({
              message: msg,
              type: code === 200 ? "success" : "error"
            });
          });
        } else {
          return false;
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.moblieBoxs {
  width: 378px;
  border: 1px solid #f5f5f5;
  height: 670px;
  background: #f5f5f5;
}
.icon {
  width: 100px;
  height: 100px;
  border-radius: 6px;
  display: block;
  float: left;
  margin-right: 10px;
  overflow: hidden;
  border: 1px solid #ccc;
  background-color: #fff;
  position: relative;
  img {
    width: 100px;
    height: 100px;
  }
}
</style>
